@echo off
chcp 65001 > nul
title Windows 系统垃圾清理工具
echo.
echo Windows 系统垃圾清理工具
echo ========================
echo.

:: 检查管理员权限
NET SESSION >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo 警告: 此程序需要管理员权限才能完全清理所有垃圾文件。
    echo 正在尝试以管理员权限重新运行...
    powershell -Command "Start-Process cmd -ArgumentList '/c %~0' -Verb RunAs"
    exit /b
)

:: 主清理流程
call :clean_temp_files
call :empty_recycle_bin
call :clean_browser_cache
call :clean_windows_update_cache
call :clean_windows_backup
call :clean_log_files
call :clean_defender_files
call :clean_iis_logs

echo.
echo 所有清理操作已完成！
pause
exit /b

:: 清理临时文件
:clean_temp_files
echo.
echo 正在清理临时文件...
echo 使用系统磁盘清理工具...
cleanmgr /sagerun:1

echo 清理系统临时目录...
del /f /s /q "%TEMP%\*" > nul
del /f /s /q "%windir%\Temp\*" > nul
del /f /s /q "%userprofile%\AppData\Local\Temp\*" > nul

echo 清理预取文件...
del /f /s /q "%windir%\Prefetch\*" > nul
exit /b

:: 清空回收站
:empty_recycle_bin
echo.
echo 正在清空回收站...
powershell -Command "Clear-RecycleBin -Force" > nul
exit /b

:: 清理浏览器缓存
:clean_browser_cache
echo.
echo 正在清理浏览器缓存...
del /f /s /q "%userprofile%\AppData\Local\Google\Chrome\User Data\Default\Cache\*" > nul
del /f /s /q "%userprofile%\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" > nul
del /f /s /q "%userprofile%\AppData\Roaming\Mozilla\Firefox\Profiles\*.default\cache2\*" > nul
exit /b

:: 清理Windows更新缓存
:clean_windows_update_cache
echo.
echo 正在清理Windows更新缓存...
net stop wuauserv > nul
del /f /s /q "%windir%\SoftwareDistribution\Download\*" > nul
net start wuauserv > nul
dism /online /cleanup-image /startcomponentcleanup /resetbase > nul
exit /b

:: 清理Windows备份文件
:clean_windows_backup
echo.
echo 正在清理Windows备份文件...
if exist "C:\Windows.old" (
    echo 清理Windows.old文件夹...
    takeown /f "C:\Windows.old" /r /d y > nul
    icacls "C:\Windows.old" /grant administrators:F /t > nul
    rmdir /s /q "C:\Windows.old" > nul
)
echo 清理系统还原点...
vssadmin delete shadows /all /quiet > nul
exit /b

:: 清理系统日志文件
:clean_log_files
echo.
echo 正在清理系统日志文件...
wevtutil cl Application > nul
wevtutil cl System > nul
del /f /s /q "%windir%\Logs\CBS\*" > nul
exit /b

:: 清理Windows Defender文件
:clean_defender_files
echo.
echo 正在清理Windows Defender文件...
cleanmgr /sagerun:4 > nul
exit /b

:: 清理IIS日志
:clean_iis_logs
echo.
echo 正在清理IIS日志...
if exist "%systemdrive%\inetpub\logs" (
    del /f /s /q "%systemdrive%\inetpub\logs\*" > nul
)
exit /b